package net.csdn.business.discuss.api.facade;

import lombok.extern.slf4j.Slf4j;
import net.csdn.business.discuss.api.enums.AutditActionEventEnum;
import net.csdn.business.discuss.api.enums.DiscussActionEventEnum;
import net.csdn.business.discuss.api.model.dto.*;
import net.csdn.business.discuss.api.model.vo.DiscussCategoryVO;
import net.csdn.business.discuss.api.model.vo.DiscussSectionVO;
import net.csdn.business.discuss.api.model.vo.DiscussVO;
import net.csdn.business.discuss.api.service.*;
import net.csdn.business.discuss.common.enums.ResultCodeEnum;
import net.csdn.business.discuss.common.exception.BusinessException;
import net.csdn.business.discuss.common.model.dto.SendDiscussAuditDTO;
import net.csdn.business.discuss.common.model.dto.SendDiscussCategoryAuditDTO;
import net.csdn.business.discuss.common.model.dto.SendDiscussSectionAuditDTO;
import net.csdn.business.discuss.common.model.dto.UserRoleDto;
import net.csdn.business.discuss.feign.api.OrgFeignClient;
import net.csdn.business.discuss.feign.api.ProjectFeignClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName DiscussWrapperFacade
 * @Description TODO
 * @Author zhangyl
 * @Date 2023/7/29 17:02
 */
@Slf4j
@Component
public class DiscussWrapperFacade {

    @Autowired
    private IDiscussService discussService;

    @Autowired
    private IDiscussActivityService discussActivityService;

    @Autowired
    private IDiscussCategoryService discussCategoryService;

    @Autowired
    private IDiscussSectionService discussSectionService;

    @Autowired
    private IDiscussAuditService discussAuditService;

    @Autowired
    private OrgFeignClient orgFeignClient;

    @Autowired
    private ProjectFeignClient projectFeignClient;



    public String add(NewDiscussDTO params){
        String currUserId=params.getCurrUserId();
        //判断权限
        Integer acessLevel=0;
        Integer sourceType=params.getSourceType();
        String sourceId=params.getSourceId();
        Map<String,String> result=discussService.insertDiscuss(params);
        //发送discuss创建的事件
        String id=result.get("id");
        String serialNumber=result.get("serialNumber");
        DiscussCreateActivityDTO createActivity=new DiscussCreateActivityDTO();
        createActivity.setDiscussId(id);
        createActivity.setSourceId(sourceId);
        createActivity.setActionUid(currUserId);
        createActivity.setSourceType(sourceType);
        createActivity.setCommentAts(params.getCommentAts());
        DiscussActivityEventDTO eventDTO=DiscussActivityEventDTO.build(DiscussActionEventEnum.CREATE_DISCUSS,createActivity);
        discussActivityService.sendDiscussActivity(eventDTO);
        //发送discuss艾特用户事件
        /*List<String> commentAts=params.getCommentAts();
        if(commentAts!=null&&commentAts.size()>0){
            DiscussAtActivityDTO atActivityDTO=new DiscussAtActivityDTO();
            atActivityDTO.setDiscussId(id);
            atActivityDTO.setSourceId(sourceId);
            atActivityDTO.setActionUid(currUserId);
            atActivityDTO.setSourceType(sourceType);
            atActivityDTO.setContent(params.getMdContent());
            atActivityDTO.setSerialNumber(serialNumber);
            atActivityDTO.setTitle(params.getTitle());
            atActivityDTO.setCommentAts(params.getCommentAts());
            DiscussActivityEventDTO atEventDTO=DiscussActivityEventDTO.build(DiscussActionEventEnum.COMMENT_AT,atActivityDTO);
            discussActivityService.sendDiscussActivity(atEventDTO);
        }*/
        //发送discuss审核
        SendDiscussAuditDTO dto=new SendDiscussAuditDTO();
        BeanUtils.copyProperties(params,dto);
        dto.setId(id);
        dto.setSourceId(sourceId);
        dto.setSourceType(sourceType);
        dto.setAction(AutditActionEventEnum.DiSCUSS_CREATE.getCode());
        dto.setCreatedBy(currUserId);
        dto.setSerialNumber(serialNumber);
        dto.setCreatedDate(new Date());
        AuditEventDTO event=AuditEventDTO.build(AutditActionEventEnum.DiSCUSS_CREATE,dto);
        discussAuditService.sendDiscussAudit(event);
        return serialNumber;
    }

    public Integer edit(UpdateDiscussDTO params){
        String id=params.getId();
        String sourceId=params.getSourceId();
        Integer sourceType=params.getSourceType();
        Integer serialNumber=params.getSerialNumber();
        if(id==null&&(StringUtils.isBlank(sourceId)&&serialNumber==null)){
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR);
        }
        DiscussVO discussVO=null;
        if(id!=null){
            discussVO=discussService.selectDiscussById(id);
            if(discussVO==null){
                throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR);
            }
        }else{
            discussVO=discussService.selectBySourceIdAndNumber(sourceId,sourceType,serialNumber);
            if(discussVO==null){
                throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR);
            }
            id=discussVO.getObjectId();
            params.setId(id);
        }
        sourceType=discussVO.getSourceType();
        sourceId=discussVO.getSourceId();
        String currUserId=params.getCurrUserId();
        //非讨论创建人，需要判断权限
        if(!discussVO.getCreatedBy().equals(currUserId)){
            //判断权限
            Integer acessLevel=0;

            UserRoleDto acessParams=new UserRoleDto();
            acessParams.setCurrUserId(currUserId);
            //判断组织或项目的访问权限
            try{
                switch (sourceType){
                    case 1:
                        acessParams.setOrgId(sourceId);
                        acessLevel=orgFeignClient.getOrgAccessLevel(acessParams);
                        break;
                    case 2:
                        acessParams.setProjectId(sourceId);
                        acessLevel=projectFeignClient.getProjectAccessLevel(acessParams);
                        break;
                }
            }catch (Exception e){
                e.printStackTrace();
                log.error("-----查询项目或组织成员的访问权限出错");
            }
            if(acessLevel<30){
                throw new BusinessException(ResultCodeEnum.UNAUTHORIZED);
            }
        }
        Integer result=discussService.updateDiscuss(params);
        if(result>0){
            //发送活动事件
            BaseActivityDTO udapateActivity=new BaseActivityDTO();
            udapateActivity.setDiscussId(id);
            udapateActivity.setSourceId(sourceId);
            udapateActivity.setActionUid(currUserId);
            udapateActivity.setSourceType(sourceType);
            udapateActivity.setCommentAts(params.getCommentAts());
            DiscussActivityEventDTO eventDTO=DiscussActivityEventDTO.build(DiscussActionEventEnum.UPDATE_DISCUSS,udapateActivity);
            discussActivityService.sendDiscussActivity(eventDTO);
           /* //发送discuss艾特用户事件
            List<String> commentAts=params.getCommentAts();
            if(commentAts!=null&&commentAts.size()>0){
                DiscussAtActivityDTO atActivityDTO=new DiscussAtActivityDTO();
                atActivityDTO.setDiscussId(id);
                atActivityDTO.setSourceId(sourceId);
                atActivityDTO.setActionUid(currUserId);
                atActivityDTO.setSerialNumber(discussVO.getSerialNumber().toString());
                atActivityDTO.setSourceType(sourceType);
                atActivityDTO.setContent(params.getMdContent());
                atActivityDTO.setTitle(params.getTitle());
                atActivityDTO.setCommentAts(params.getCommentAts());
                DiscussActivityEventDTO atEventDTO=DiscussActivityEventDTO.build(DiscussActionEventEnum.COMMENT_AT,atActivityDTO);
                discussActivityService.sendDiscussActivity(atEventDTO);
            }*/
            //发送discuss审核
            SendDiscussAuditDTO dto=new SendDiscussAuditDTO();
            BeanUtils.copyProperties(params,dto);
            dto.setId(id);
            dto.setSourceId(sourceId);
            dto.setSourceType(sourceType);
            dto.setAction(AutditActionEventEnum.DiSCUSS_UPDATE.getCode());
            dto.setCreatedBy(currUserId);
            dto.setCreatedDate(new Date());
            dto.setSerialNumber(discussVO.getSerialNumber().toString());
            AuditEventDTO event=AuditEventDTO.build(AutditActionEventEnum.DiSCUSS_UPDATE,dto);
            discussAuditService.sendDiscussAudit(event);
        }
        return result;
    }


    public Integer remove(DeleteResourceDTO params){
        String id=params.getId();
        DiscussVO discussVO=discussService.selectDiscussById(id);
        if(discussVO==null){
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM,"id");
        }
        String currUserId=params.getCurrUserId();
        String createBy=discussVO.getCreatedBy();
        Integer sourceType=discussVO.getSourceType();
        String sourceId=discussVO.getSourceId();
        if(!createBy.equals(currUserId)){
            //判断权限
            Integer acessLevel=0;
            UserRoleDto acessParams=new UserRoleDto();
            acessParams.setCurrUserId(currUserId);
            //判断组织或项目的访问权限
            try{
                switch (sourceType){
                    case 1:
                        acessParams.setOrgId(sourceId);
                        acessLevel=orgFeignClient.getOrgAccessLevel(acessParams);
                        break;
                    case 2:
                        acessParams.setProjectId(sourceId);
                        acessLevel=projectFeignClient.getProjectAccessLevel(acessParams);
                        break;
                }
            }catch (Exception e){
                e.printStackTrace();
                log.error("-----查询项目或组织成员的访问权限出错");
            }
            if(acessLevel<40){
                throw new BusinessException(ResultCodeEnum.UNAUTHORIZED);
            }
        }
        params.setSourceId(sourceId);
        Integer result=discussService.deleteDiscussById(params);
        if(result>0){
            //发送discuss审核
            SendDiscussAuditDTO dto=new SendDiscussAuditDTO();
            BeanUtils.copyProperties(discussVO,dto);
            dto.setId(id);
            dto.setSourceId(sourceId);
            dto.setSourceType(sourceType);
            dto.setSerialNumber(discussVO.getSerialNumber().toString());
            dto.setAction(AutditActionEventEnum.DiSCUSS_DELETE.getCode());
            dto.setCreatedBy(params.getCurrUserId());
            dto.setCreatedDate(new Date());
            AuditEventDTO event=AuditEventDTO.build(AutditActionEventEnum.DiSCUSS_DELETE,dto);
            discussAuditService.sendDiscussAudit(event);
        }
        return result;
    }

    public String addCategory(NewDiscussCategoryDTO params){
        String currUserId=params.getCurrUserId();
        //判断权限
        Integer acessLevel=0;
        Integer sourceType=params.getSourceType();
        String sourceId=params.getSourceId();
        UserRoleDto acessParams=new UserRoleDto();
        acessParams.setCurrUserId(currUserId);
        //判断组织或项目的访问权限
        try{
            switch (sourceType){
                case 1:
                    acessParams.setOrgId(sourceId);
                    acessLevel=orgFeignClient.getOrgAccessLevel(acessParams);
                    break;
                case 2:
                    acessParams.setProjectId(sourceId);
                    acessLevel=projectFeignClient.getProjectAccessLevel(acessParams);
                    break;
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("-----查询项目或组织成员的访问权限出错");
        }
        if(acessLevel<40){
            throw new BusinessException(ResultCodeEnum.UNAUTHORIZED);
        }
        params.setSourceId(sourceId);
        String id=discussCategoryService.insertDiscussCategory(params);
        //发送审核通知
        SendDiscussCategoryAuditDTO dto=new SendDiscussCategoryAuditDTO();
        BeanUtils.copyProperties(params,dto);
        dto.setId(id);
        dto.setSourceId(sourceId);
        dto.setSourceType(sourceType);
        dto.setAction(AutditActionEventEnum.DiSCUSS_CATEGORY_CREATE.getCode());
        dto.setCreatedBy(params.getCurrUserId());
        dto.setCreatedDate(new Date());
        AuditEventDTO event=AuditEventDTO.build(AutditActionEventEnum.DiSCUSS_CATEGORY_CREATE,dto);
        discussAuditService.sendDiscussAudit(event);
        return id;
    }

    public Integer editCategory(UpdateDiscussCategoryDTO params){
        String currUserId=params.getCurrUserId();
        String id=params.getId();
        DiscussCategoryVO discussCategoryVO=discussCategoryService.selectDiscussCategoryById(id);
        if(discussCategoryVO==null){
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM,"id");
        }
        //判断权限
        Integer acessLevel=0;
        Integer sourceType=discussCategoryVO.getSourceType();
        String sourceId=discussCategoryVO.getSourceId();
        UserRoleDto acessParams=new UserRoleDto();
        acessParams.setCurrUserId(currUserId);
        //判断组织或项目的访问权限
        try{
            switch (sourceType){
                case 1:
                    acessParams.setOrgId(sourceId);
                    acessLevel=orgFeignClient.getOrgAccessLevel(acessParams);
                    break;
                case 2:
                    acessParams.setProjectId(sourceId);
                    acessLevel=projectFeignClient.getProjectAccessLevel(acessParams);
                    break;
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("-----查询项目或组织成员的访问权限出错");
        }
        if(acessLevel<40){
            throw new BusinessException(ResultCodeEnum.UNAUTHORIZED);
        }
        params.setSourceId(sourceId);
        Integer result=discussCategoryService.updateDiscussCategory(params);
        //发送审核通知
        SendDiscussCategoryAuditDTO dto=new SendDiscussCategoryAuditDTO();
        BeanUtils.copyProperties(params,dto);
        dto.setId(params.getId());
        dto.setSourceId(sourceId);
        dto.setSourceType(sourceType);
        dto.setAction(AutditActionEventEnum.DiSCUSS_CATEGORY_UPDATE.getCode());
        dto.setCreatedBy(params.getCurrUserId());
        dto.setCreatedDate(new Date());
        AuditEventDTO event=AuditEventDTO.build(AutditActionEventEnum.DiSCUSS_CATEGORY_UPDATE,dto);
        discussAuditService.sendDiscussAudit(event);
        return result;
    }


    public Integer deleteCategory(DeleteResourceDTO params){
        String currUserId=params.getCurrUserId();
        DiscussCategoryVO discussCategoryVO=discussCategoryService.selectDiscussCategoryById(params.getId());
        if(discussCategoryVO==null){
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM,"id");
        }
        //判断权限
        Integer acessLevel=0;
        Integer sourceType=discussCategoryVO.getSourceType();
        String sourceId=discussCategoryVO.getSourceId();
        UserRoleDto acessParams=new UserRoleDto();
        acessParams.setCurrUserId(currUserId);
        //判断组织或项目的访问权限
        try{
            switch (sourceType){
                case 1:
                    acessParams.setOrgId(sourceId);
                    acessLevel=orgFeignClient.getOrgAccessLevel(acessParams);
                    break;
                case 2:
                    acessParams.setProjectId(sourceId);
                    acessLevel=projectFeignClient.getProjectAccessLevel(acessParams);
                    break;
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("-----查询项目或组织成员的访问权限出错");
        }
        if(acessLevel<40){
            throw new BusinessException(ResultCodeEnum.UNAUTHORIZED);
        }
        params.setSourceType(discussCategoryVO.getSourceType());
        params.setSourceId(sourceId);
        Integer result=discussCategoryService.deleteDiscussCategoryById(params);
        //发送审核通知
        SendDiscussCategoryAuditDTO dto=new SendDiscussCategoryAuditDTO();
        BeanUtils.copyProperties(discussCategoryVO,dto);
        dto.setId(params.getId());
        dto.setSourceId(sourceId);
        dto.setSourceType(sourceType);
        dto.setAction(AutditActionEventEnum.DiSCUSS_CATEGORY_DELETE.getCode());
        dto.setCreatedBy(params.getCurrUserId());
        dto.setCreatedDate(new Date());
        AuditEventDTO event=AuditEventDTO.build(AutditActionEventEnum.DiSCUSS_CATEGORY_DELETE,dto);
        discussAuditService.sendDiscussAudit(event);
        return result;
    }


    public String addSection(NewDiscussSectionDTO params){
        String currUserId=params.getCurrUserId();
        //判断权限
        Integer acessLevel=0;
        Integer sourceType=params.getSourceType();
        String sourceId=params.getSourceId();
        UserRoleDto acessParams=new UserRoleDto();
        acessParams.setCurrUserId(currUserId);
        //判断组织或项目的访问权限
        try{
            switch (sourceType){
                case 1:
                    acessParams.setOrgId(sourceId);
                    acessLevel=orgFeignClient.getOrgAccessLevel(acessParams);
                    break;
                case 2:
                    acessParams.setProjectId(sourceId);
                    acessLevel=projectFeignClient.getProjectAccessLevel(acessParams);
                    break;
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("-----查询项目或组织成员的访问权限出错");
        }
        if(acessLevel<40){
            throw new BusinessException(ResultCodeEnum.UNAUTHORIZED);
        }
        String id=discussSectionService.insertDiscussSection(params);
        //发送审核通知
        SendDiscussSectionAuditDTO dto=new SendDiscussSectionAuditDTO();
        BeanUtils.copyProperties(params,dto);
        dto.setId(id);
        dto.setSourceId(sourceId);
        dto.setSourceType(sourceType);
        dto.setAction(AutditActionEventEnum.DiSCUSS_SECTION_CREATE.getCode());
        dto.setCreatedBy(params.getCurrUserId());
        dto.setCreatedDate(new Date());
        AuditEventDTO event=AuditEventDTO.build(AutditActionEventEnum.DiSCUSS_SECTION_CREATE,dto);
        discussAuditService.sendDiscussAudit(event);
        return id;
    }

    public Integer editSection(UpdateDiscussSectionDTO params){
        String id=params.getId();
        String currUserId=params.getCurrUserId();
        DiscussSectionVO sectionVO=discussSectionService.selectDiscussSectionById(id);
        if(sectionVO==null){
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM,"id");
        }
        //判断权限
        Integer acessLevel=0;
        Integer sourceType=sectionVO.getSourceType();
        String sourceId=sectionVO.getSourceId();
        UserRoleDto acessParams=new UserRoleDto();
        acessParams.setCurrUserId(currUserId);
        //判断组织或项目的访问权限
        try{
            switch (sourceType){
                case 1:
                    acessParams.setOrgId(sourceId);
                    acessLevel=orgFeignClient.getOrgAccessLevel(acessParams);
                    break;
                case 2:
                    acessParams.setProjectId(sourceId);
                    acessLevel=projectFeignClient.getProjectAccessLevel(acessParams);
                    break;
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("-----查询项目或组织成员的访问权限出错");
        }
        if(acessLevel<40){
            throw new BusinessException(ResultCodeEnum.UNAUTHORIZED);
        }
        params.setSourceId(sourceId);
        Integer result=discussSectionService.updateDiscussSection(params);
        //发送审核通知
        SendDiscussSectionAuditDTO dto=new SendDiscussSectionAuditDTO();
        BeanUtils.copyProperties(params,dto);
        dto.setId(params.getId());
        dto.setSourceId(sourceId);
        dto.setSourceType(sourceType);
        dto.setAction(AutditActionEventEnum.DiSCUSS_SECTION_UPDATE.getCode());
        dto.setCreatedBy(params.getCurrUserId());
        dto.setCreatedDate(new Date());
        AuditEventDTO event=AuditEventDTO.build(AutditActionEventEnum.DiSCUSS_SECTION_UPDATE,dto);
        discussAuditService.sendDiscussAudit(event);
        return result;
    }


    public Integer deleteSection(DeleteResourceDTO params){
        DiscussSectionVO discussSectionVO=discussSectionService.selectDiscussSectionById(params.getId());
        if(discussSectionVO==null){
            throw new BusinessException(ResultCodeEnum.PARAMETER_ERROR_CUSTOM,"id");
        }
        String currUserId=params.getCurrUserId();
        //判断权限
        Integer acessLevel=0;
        Integer sourceType=discussSectionVO.getSourceType();
        String sourceId=discussSectionVO.getSourceId();
        UserRoleDto acessParams=new UserRoleDto();
        acessParams.setCurrUserId(currUserId);
        //判断组织或项目的访问权限
        try{
            switch (sourceType){
                case 1:
                    acessParams.setOrgId(sourceId);
                    acessLevel=orgFeignClient.getOrgAccessLevel(acessParams);
                    break;
                case 2:
                    acessParams.setProjectId(sourceId);
                    acessLevel=projectFeignClient.getProjectAccessLevel(acessParams);
                    break;
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("-----查询项目或组织成员的访问权限出错");
        }
        if(acessLevel<40){
            throw new BusinessException(ResultCodeEnum.UNAUTHORIZED);
        }
        params.setSourceType(discussSectionVO.getSourceType());
        params.setSourceId(sourceId);
        Integer result=discussSectionService.deleteDiscussSectionById(params);
        //发送审核通知
        SendDiscussSectionAuditDTO dto=new SendDiscussSectionAuditDTO();
        BeanUtils.copyProperties(discussSectionVO,dto);
        dto.setId(params.getId());
        dto.setSourceId(sourceId);
        dto.setSourceType(sourceType);
        dto.setAction(AutditActionEventEnum.DiSCUSS_SECTION_DELETE.getCode());
        dto.setCreatedBy(params.getCurrUserId());
        dto.setCreatedDate(new Date());
        AuditEventDTO event=AuditEventDTO.build(AutditActionEventEnum.DiSCUSS_SECTION_DELETE,dto);
        discussAuditService.sendDiscussAudit(event);
        return result;
    }
}
